﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;

public class UnitManager:Singleton<UnitManager>
{
    public GameObject UnitGameObjects; 
    public Dictionary<string, XmlUnit> xmlUnits  = new Dictionary<string,XmlUnit>();
    public Dictionary<string, Unit> Units = new Dictionary<string, Unit>();
    public string XmlPath ;
    public void InitFormXml() 
    {
        XmlPath = Game.ConfigPath + "Units.xml";
        string strUnit = File.ReadAllText(XmlPath);
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(strUnit);
        XmlNode node =  doc.ChildNodes[0];
        for (int i = 0; i < node.ChildNodes.Count;i++)
        {
            XmlUnit xmlUnit = new XmlUnit();
            xmlUnit.parse(node.ChildNodes[i]);
            this.xmlUnits[xmlUnit.Name] = xmlUnit;
        }

        
    }

    public Unit GetUnit(string unitID) 
    {
      return this.Units[unitID];
    }
    public void InitRootGameObject() 
    {
        UnitGameObjects = new GameObject("Unit-GameObject");
        UnitGameObjects.AddComponent<UIPanel>();
        
    }
    public Unit CreateUnit(string unitName,string unitId,int playerId=-1) 
    {
        Debug.Log("CreateUnit Name:"+unitName+" Id:"+unitId);
        GameObject UnitObj = new GameObject(unitId);
        Units[unitId] = (Unit)UnitObj.AddComponent(xmlUnits[unitName].UnitType);
        if (playerId != -1)
        {
            Units[unitId].GamePlayerId = playerId;
        }
        Units[unitId].xmlUnit = xmlUnits[unitName];
        Units[unitId].Id = unitId;
        UnitObj.transform.parent = this.UnitGameObjects.transform;
        UnitObj.transform.localScale = Vector3.one;
        UnitObj.transform.localPosition = Vector3.zero;
        UnitObj.transform.localRotation = Quaternion.identity;
        return Units[unitId]; 
    }

    public Unit CreateUnit(string unitName) 
    {
        GameObject UnitObj = new GameObject(unitName);
        Unit unit =  (Unit)UnitObj.AddComponent(xmlUnits[unitName].UnitType);
        unit.xmlUnit = xmlUnits[unitName];
        unit.Id = System.DateTime.Now.Ticks.ToString();
        UnitObj.transform.parent = this.UnitGameObjects.transform;
        UnitObj.transform.localScale = Vector3.one;
        UnitObj.transform.localPosition = Vector3.zero;
        UnitObj.transform.localRotation = Quaternion.identity;
        unit.CallEvent("UnitCreate");
        unit.OnLoad();
        return unit;
    }

    public void AddMover(Unit unit,string moverName) 
    {
        XmlNode node = MoverManager.Instance.GetXmlMover(moverName);
        Mover mover = (Mover)unit.gameObject.AddComponent(node.Name);
        mover.Init(node);
    }

}

public class XmlUnit 
{
    public string UnitType;
    public string ShowName;
    public string Name;
    public int HP;
    public int MP;
    public int MoveSpeed;
    public XmlNode Abilities;
    public XmlNode Actors;
    public XmlNode Weapons;
    public Dictionary<string, bool> Flags;

    public void parse(XmlNode node)  
    {
        this.UnitType = node.Name;
        if (node.Attributes["ShowName"] != null)
        {
            this.ShowName = node.Attributes["ShowName"].Value;
        }
        this.Name = node.Attributes["Name"].Value;
        if (node.Attributes["HP"] != null)
        {
            this.HP = int.Parse(node.Attributes["HP"].Value);
        }
        if (node.Attributes["MP"] != null)
        {
            this.MP = int.Parse(node.Attributes["MP"].Value);
        }
        if (node.Attributes["MoveSpeed"] != null)
        {
            this.MoveSpeed = int.Parse(node.Attributes["MoveSpeed"].Value);
        }

        XmlNode flagNode = node.SelectSingleNode("Flags");
        if (flagNode != null) 
        {
            this.Flags = new Dictionary<string, bool>();
            foreach (XmlNode item in flagNode.ChildNodes) 
            {
                this.Flags[item.Name] = bool.Parse(item.InnerText);
            }
        }
        this.Abilities = node.SelectSingleNode("Abilities");
        this.Actors = node.SelectSingleNode("Actors");
        this.Weapons = node.SelectSingleNode("Weapons");
    }
}
